package com.ruoyi.express.third.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Libs {

	/** / **/
	public static final String MARK_SLASH = "/";

	/** = **/
	public static final String MARK_EQUAL = "=";

	/** - **/
	public static final String MARK_HYPHEN = "-";

	/** _ **/
	public static final String MARK_DASH = "_";

	/** & **/
	public static final String MARK_AND = "&";

	/** , **/
	public static final String MARK_COMMA = ",";

	/** . **/
	public static final String MARK_POINT = ".";

	/** ? **/
	public static final String MARK_QUESTION = "?";

	/** : **/
	public static final String MARK_COLON = ":";
	
	/** | **/
	public static final String MARK_VERTICAL = "|";
	
	/** EMPTY **/
	public static final String EMPTY = "";

	/** UTF-8 **/
	public static final String UTF_8 = "UTF-8";

	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static String getSuffix(String url) {
		return getSuffix(url, "\\" + MARK_POINT);
	}

	public static String getSuffix(String url, String split) {
		String[] str = url.split(split);
		if (str.length == 1) {
			return "";
		}
		return str[str.length - 1];
	}

	public static String getBody(InputStream input) throws IOException {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = input.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		return out.toString();
	}
	
	public static final String getHost(String url) {
		Pattern pattern = Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
		Matcher m = pattern.matcher(url);
		if (m.find()) {
			return m.group();
		}
		return null;
	}
	
	public static String htmlEncode(String source) {
        if (source == null) {
            return "";
        }
        String html = "";
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            switch (c) {
            case '<':
                buffer.append("&lt;");
                break;
            case '>':
                buffer.append("&gt;");
                break;
            case '&':
                buffer.append("&amp;");
                break;
            case '"':
                buffer.append("&quot;");
                break;
            case 10:
            case 13:
                break;
            default:
                buffer.append(c);
            }
        }
        html = buffer.toString();
        return html;
    }
}
